package com.graduation.clientback.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.graduation.clientback.common.R;
import com.graduation.clientback.domain.Paper;
import com.graduation.clientback.domain.Work;
import com.graduation.clientback.service.PaperService;
import com.graduation.clientback.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    @Autowired
    private WorkService workService;

    /**
     * 查看论文
     * @param workId
     * @return
     */
    @GetMapping("/detail")
    public R<Paper> detail(Long workId){
        LambdaQueryWrapper<Paper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Paper::getWorkId, workId);
        Paper paper = paperService.getOne(queryWrapper);
        return R.success(paper);
    }

    /**
     * 提交论文
     * @param paper
     * @return
     */
    @PutMapping("/update")
    public R<String> handlePaper(@RequestBody Paper paper){
        LambdaQueryWrapper<Paper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Paper::getWorkId, paper.getWorkId());
        Paper existPaper = paperService.getOne(queryWrapper);
        // 不存在
        if(existPaper == null) {
            paper.setCreateTime(LocalDateTime.now());
            paper.setUpdateTime(LocalDateTime.now());
            paperService.save(paper);
            Work work = workService.getById(paper.getWorkId());
            work.setProcess(work.getProcess() + 33);
            workService.updateById(work);
        } else {
            // 存在
            paper.setUpdateTime(LocalDateTime.now());
            paperService.updateById(paper);
        }
        return R.success("提交成功");
    }
}
